import { createApp } from 'vue';
import GlobalLoading from '../components/GlobalLoading.vue';

let loadingInstance = null;
let loadingCount = 0;

/**
 * 创建全局加载实例
 */
function createLoadingInstance() {
  if (loadingInstance) return;
  
  // 创建挂载点
  const mountNode = document.createElement('div');
  document.body.appendChild(mountNode);
  
  // 创建加载组件实例
  const app = createApp(GlobalLoading);
  
  // 挂载应用
  loadingInstance = app.mount(mountNode);
}

/**
 * 显示全局加载
 */
export function showLoading() {
  if (!loadingInstance) {
    createLoadingInstance();
  }
  
  loadingCount++;
  loadingInstance.show();
}

/**
 * 隐藏全局加载
 */
export function hideLoading() {
  if (!loadingInstance) return;
  
  loadingCount--;
  if (loadingCount <= 0) {
    loadingCount = 0;
    loadingInstance.hide();
  }
}

/**
 * 全局加载服务
 */
export const LoadingService = {
  show: showLoading,
  hide: hideLoading
};

export default LoadingService;